package com.example.chessbattleplatform.web.chess_rooms.dataService;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.chessbattleplatform.Pojo.Exception.BusinessException;
import com.example.chessbattleplatform.Pojo.Exception.StatusCode;
import com.example.chessbattleplatform.web.chess_rooms.Pojo.Enum.RoomTypeEnum;
import com.example.chessbattleplatform.web.chess_rooms.Pojo.mappers.Rooms;
import com.example.chessbattleplatform.web.chess_rooms.dataService.impl.RoomsDataServiceInterface;
import com.example.chessbattleplatform.web.chess_rooms.mapper.RoomMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service
@RequiredArgsConstructor
public class RoomsDataService extends ServiceImpl<RoomMapper, Rooms> implements RoomsDataServiceInterface {
    private final RoomMapper roomMapper;

    @Override
    public Rooms createRoom(Rooms rooms) {
        int insert = roomMapper.insert(rooms);

        if (insert <= 0) {
            throw new BusinessException(StatusCode.ERROR_CODE, "新增房间失败,请联系客服!");
        }

        return rooms;
    }

    @Override
    public Rooms getRoom(String roomUuid) {
        Rooms rooms = roomMapper.selectById(roomUuid);

        if (rooms != null) {
            return rooms;
        }

        throw new BusinessException(StatusCode.ERROR_CODE, "房间不存在,请联系客服!");
    }

    @Override
    public Rooms updateRoom(Rooms rooms) {
        return null;
    }

    /**
     * 列表查询房间
     * @param Current current – 当前页
     * @param limit limit – 每页显示条数
     * @return Page<Rooms> - 房间列表数据
     */
    @Override
    public Page<Rooms> getAllProductsKind(Long Current, Long limit) {
        // 查询所有数据
        // 第page 页，每页limit 条数据。
        // 返回分页结果
        // LambdaQueryWrapper<Rooms> lambdaQueryWrapper = new LambdaQueryWrapper<>(Rooms.class)
        //         .eq(Rooms::getRoomType, RoomTypeEnum.PUBLIC);

        LambdaQueryWrapper<Rooms> lambdaQueryWrapper = new LambdaQueryWrapper<>(Rooms.class);
        return roomMapper.selectPage(new Page<>(Current, limit), lambdaQueryWrapper);
    }

    @Override
    public Boolean deleteWaitingRoom(String roomId) {
        LambdaQueryWrapper<Rooms> lambdaQueryWrapper = new LambdaQueryWrapper<>(Rooms.class)
                .eq(Rooms::getRoomUuid, roomId);

        return this.remove(lambdaQueryWrapper);
    }

    @Override
    public Boolean IsMyRoom(String roomId) {
        LambdaQueryWrapper<Rooms> lambdaQueryWrapper = new LambdaQueryWrapper<>(Rooms.class)
                .eq(Rooms::getRoomUuid, roomId);

        return this.exists(lambdaQueryWrapper);
    }

    @Override
    public Boolean IsRoomTypeEnum(String roomId, RoomTypeEnum roomTypeEnum) {
        LambdaQueryWrapper<Rooms> lambdaQueryWrapper = new LambdaQueryWrapper<>(Rooms.class)
                .allEq(Map.of(Rooms::getRoomUuid, roomId,Rooms::getRoomType,roomTypeEnum));

        return this.exists(lambdaQueryWrapper);
    }

    @Override
    public Boolean IsMyRoomPassword(String roomId, String password) {
        LambdaQueryWrapper<Rooms> lambdaQueryWrapper = new LambdaQueryWrapper<>(Rooms.class)
                .allEq(Map.of(Rooms::getRoomUuid, roomId,Rooms::getRoomPassword,password));

        return this.exists(lambdaQueryWrapper);
    }


}
